package cn.formaggie.controller.manage;

import cn.formaggie.annotation.CheckTwiceSubmit;
import cn.formaggie.controller.BaseController;
import cn.formaggie.entity.model.PageBean;
import cn.formaggie.entity.model.roleandpermission.Worker;
import cn.formaggie.service.face.RoleService;
import cn.formaggie.service.face.WorkerService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/back/worker")
public class WorkerManageController extends BaseController {

    @Autowired
    private WorkerService workerService;

    @Autowired
    private RoleService roleService;

    @RequiresPermissions({"工人管理"})
    @RequestMapping("/list")
    public ModelAndView workerList(@ModelAttribute(STATE_MSG)String stateMsg){
        ModelAndView mav=new ModelAndView();
        mav.addObject(STATE_MSG,stateMsg);
        mav.addObject("roles",roleService.findAllRoles());
        mav.addObject("mainPage",BACK_PAGE_PATH+"/manager/worker/list.jsp");
        mav.setViewName("/backpage/backTemp");
        return mav;
    }

    @RequiresPermissions({"工人管理"})
    @RequestMapping("/load")
    @ResponseBody
    public Map load(@RequestParam(value = "s_workerId",required = false) String s_workerId,
                    @RequestParam(value = "s_workerName",required = false) String s_workerName,
                    @RequestParam(value = "page",required = false) Integer page,
                    @RequestParam(value = "limit",required = false) Integer limit){

        PageBean pageBean=new PageBean(page,limit);
        Map<String,Object> sqlMap=new HashMap<>();
        sqlMap.put("workerId",s_workerId);
        sqlMap.put("name",s_workerName);
        sqlMap.put("pageBean",pageBean);

        List<Worker> data=workerService.findWorkers(sqlMap);
        Integer count=workerService.getWorkersCount(sqlMap);

        Map<String,Object> viewMap=new HashMap<>();
        viewMap.put("data",data);
        viewMap.put("count",count);
        viewMap.put("code",0);
        viewMap.put("msg","");
        return viewMap;
    }

    @RequiresPermissions({"工人管理"})
    @RequestMapping("/add")
    @CheckTwiceSubmit
    public String addWorker(Worker worker, RedirectAttributes model){
        String state=workerService.addWorker(worker)?"添加成功":"添加失败,请检查工号是否已经存在";
        model.addFlashAttribute(STATE_MSG,state);
        return "redirect:/back/worker/list";
    }

    @RequiresPermissions({"工人管理"})
    @RequestMapping("/modifyInfo")
    @CheckTwiceSubmit
    public String modifyWorkerInfo(Worker worker,RedirectAttributes model){
        String state=workerService.modifyWorkerInfo(worker)?"修改成功":"修改失败";
        model.addFlashAttribute(STATE_MSG,state);
        return "redirect:/back/worker/list";
    }

    @RequiresPermissions({"工人管理"})
    @RequestMapping("/delete")
    @ResponseBody
    public boolean deleteWorker(Integer id){
        if(id==1){
            return false;
        }
       return workerService.deleteWorkerByWorkerId(id);
    }


}
